﻿using ShengYueWorkShop.BLL.PublicHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShengYueWorkShop.Server
{
    public partial class Form1 : Form
    {
        private SqlHelper _SqlHepler { get; set; } = new SqlHelper();
        private DataTable _FtpServerData { get; set; } = new DataTable();
        private FtpHelper _FtpHelper { get; set; }
        public Form1()
        {
            InitializeComponent();
            this.Shown += Form1_Shown;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            KJ_ListView.GridLines = true;//显示网格线
            KJ_ListView.FullRowSelect = true;//选择整行

            if (LoadAppConfig())
            {
                KJ_GetFtpDataBtn_Click(null, null);
                KJ_GetFtpDataBtn.Click += KJ_GetFtpDataBtn_Click;
                KJ_SqlServerBtn.Click += KJ_SqlServerBtn_Click;

                KJ_ListView.DoubleClick += KJ_ListView_DoubleClick;
                KJ_ListView.SelectedIndexChanged += KJ_ListView_SelectedIndexChanged;
                //KJ_FIleListBox.DoubleClick += KJ_FIleListBox_DoubleClick;
                //KJ_FIleListBox.SelectedIndexChanged += KJ_FIleListBox_SelectedIndexChanged;

                KJ_PicBtn1.Click += KJ_PicBtn_Click;
                KJ_PicBtn2.Click += KJ_PicBtn_Click;
                KJ_PicBtn3.Click += KJ_PicBtn_Click;
                KJ_PicBtn4.Click += KJ_PicBtn_Click;
            }
        }
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <returns></returns>
        private bool LoadAppConfig()
        {
            try
            {
                SqlHelper._SqlConn = AppConfig.GetValue_Decrypt("SqlConn");
                if (SqlHelper._SqlConn.Length == 0)
                {
                    return false;
                }
                return true;
            }
            catch
            {
                MessageBox.Show("读取配置文件错误!");
                return false;
            }
        }
        private void KJ_GetFtpDataBtn_Click(object sender, EventArgs e)
        {
            _FtpServerData = _SqlHepler.GetFtpServer();
            KJ_ListView.Items.Clear();
            string fFtpServer;
            if (_FtpServerData.Rows.Count > 0)
            {
                ListViewItem fListItem;
                for (int i = 0; i < _FtpServerData.Rows.Count; i++)
                {
                    fFtpServer = string.Format("{0}|{1}", _FtpServerData.Rows[i]["MachineID"], _FtpServerData.Rows[i]["ServerIP"]);
                    fListItem = new ListViewItem();
                    fListItem.ImageIndex = 0;
                    fListItem.SubItems.Add((i + 1).ToString());
                    fListItem.SubItems.Add("机台");
                    fListItem.SubItems.Add(fFtpServer);
                    KJ_ListView.Items.Add(fListItem);
                }
            }
        }
        private void KJ_SqlServerBtn_Click(object sender, EventArgs e)
        {
            SqlView frm = new SqlView();
            frm.ShowDialog();
        }
        private void KJ_ListView_DoubleClick(object sender, EventArgs e)
        {
            if (KJ_ListView.SelectedItems.Count > 0)
            {
                ListViewItem fListItem = KJ_ListView.SelectedItems[0];
                if (fListItem.ImageIndex == 0)
                {
                    DataRow fRow = _FtpServerData.Rows[fListItem.Index];
                    _FtpHelper = new FtpHelper()
                    {
                        Uri = new Uri("ftp://" + fRow["ServerIP"].ToString() + ":" + fRow["ServerPort"].ToString()),
                        UserName = fRow["UID"].ToString(),
                        Password = fRow["PWD"].ToString()
                    };
                    try
                    {
                        GetFileList();
                    }
                    catch
                    {
                        MessageBox.Show("FTP服务器连接失败");
                    }
                }
                else if (fListItem.ImageIndex == 1)
                {
                    _FtpHelper.GotoDirectory(fListItem.SubItems[3].Text);
                    GetFileList();
                }
                else if (fListItem.ImageIndex == 3)
                {
                    //返回上一步
                    try
                    {
                        if(_FtpHelper.ComeoutDirectory())
                        {
                            GetFileList();
                        }
                    }
                    catch 
                    {
                        MessageBox.Show("已经是根目录");
                    }
                }
            }
        }
        private void KJ_ListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (KJ_ListView.SelectedItems.Count > 0)
            {
                string fFileName;
                ListViewItem fListItem = KJ_ListView.SelectedItems[0];
                if (fListItem.ImageIndex == 2)
                {
                    fFileName = fListItem.SubItems[3].Text;
                    if (fFileName.Contains(".jpg") || fFileName.Contains(".bmp"))
                    {
                        // KJ_PicControl.Image = Image.FromStream(_FtpHelper.GetMemoryStream(fFileName));
                        string fPicPatch = _FtpHelper.GetJpgPatch(fFileName);
                        KJ_PicControl.LoadAsync(fPicPatch);
                    }
                }
                label1.Text = string.Format("{0}/{1}", fListItem.Index, KJ_ListView.Items.Count);
            }
        }
        private void GetFileList()
        {
            FileStruct[] fFileSutctList = _FtpHelper.ListFilesAndDirectories();
            label2.Text = _FtpHelper.DirectoryPath;
            KJ_ListView.Items.Clear();
            ListViewItem fListItem = new ListViewItem();
            fListItem.ImageIndex = 3;
            fListItem.SubItems.Add("0");
            fListItem.SubItems.Add("操作");
            fListItem.SubItems.Add("返回上一层");
            KJ_ListView.Items.Add(fListItem);
            for (int i = 0; i < fFileSutctList.Length; i++)
            {
                fListItem = new ListViewItem();
                fListItem.SubItems.Add((i + 1).ToString());
                if (fFileSutctList[i].IsDirectory)
                {
                    fListItem.ImageIndex = 1;
                    fListItem.SubItems.Add("文件夹");
                }
                else
                {
                    fListItem.ImageIndex = 2;
                    fListItem.SubItems.Add("文件");
                }
                fListItem.SubItems.Add(fFileSutctList[i].Name);
                KJ_ListView.Items.Add(fListItem);
            }
            label1.Text = string.Format("0/{0}", KJ_ListView.Items.Count);
        }
        private void KJ_PicBtn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (KJ_ListView.Items.Count > 0)
            {
                int fIndex = 0;
                if (KJ_ListView.SelectedItems.Count >= 1)
                {
                    fIndex = KJ_ListView.SelectedItems[1].Index;
                }
                else
                {
                    return;
                }

                if ("首张" == btn.Text)
                {
                    if (fIndex > 0)
                    {
                        KJ_ListView.SelectedIndices.Clear();
                        KJ_ListView.SelectedIndices.Add(1);
                    }
                }
                else if ("上一张" == btn.Text)
                {

                    if (fIndex == 0)
                    {
                        MessageBox.Show("已经是第一张");
                        return;
                    }
                    else
                    {
                        fIndex--;
                        KJ_ListView.SelectedIndices.Clear();
                        KJ_ListView.SelectedIndices.Add(fIndex);
                    }
                }
                else if ("下一张" == btn.Text)
                {
                    fIndex++;
                    if (fIndex == KJ_ListView.Items.Count - 1)
                    {
                        MessageBox.Show("已经是最后一张");
                        return;
                    }
                    else
                    {
                        KJ_ListView.SelectedIndices.Clear();
                        KJ_ListView.SelectedIndices.Add(fIndex);
                    }
                }
                else if ("末张" == btn.Text)
                {
                    fIndex = KJ_ListView.Items.Count - 1;
                    KJ_ListView.SelectedIndices.Clear();
                    KJ_ListView.SelectedIndices.Add(fIndex);
                }
            }


        }
    }
}
